<?php

class VehicleController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
         $this->_helper->layout->setLayout('layout-mantenimientos');
    }

    public function indexAction()
    {
        // action body
         try{
          
            $customers = new Application_Model_DbTable_Vehicle();
            $this->view->vehiculos = $customers->find();
            if ($this->getRequest()->isPost()) {
                $placa = $_POST['placa'];
                $marca = $_POST['marca'];
                if(!empty($placa) || !empty($marca)){                    
                    $this->view->vehiculos = $customers->findVehicle($placa, $marca);
                }
            }
        }
        catch(Exception $e){
        
          echo $e;
            throw $e;
        }
    }

    public function addAction()
    {
        // action body
          if ($this->getRequest()->isPost()) 
        {
            if(!empty($_POST['placa'])){
                $placa = $_POST['placa'];
                $anno = $_POST['anno'];
                $marca = $_POST['marca']; 
                $modelo = $_POST['modelo']; 
                $motor = $_POST['motor'];
                $observaciones = $_POST['observaciones'];
               
                $customers = new Application_Model_DbTable_Vehicle();
                $customers->addVehicle($placa, $anno, $marca, $modelo,$motor,$observaciones,'N');

            }
        }          
    }

    public function editAction()
    {
        // action body
        $placa = $this->_getParam('placa', 0);
        if ($placa != null) {
            try{
                $customers = new Application_Model_DbTable_Vehicle();                                
                $this->view->vehicle = $customers->getVehicle($placa);
            }
            catch(Exception $e){
                throw $e;
            }             

            if(!empty($_POST['placa'])){   
                  echo "if post". $id;
                $placa = $_POST['placa'];
                $anno = $_POST['anno'];
                $marca = $_POST['marca'];
                $modelo = $_POST['modelo'];
                $motor = $_POST['motor']; 
                $observaciones = $_POST['observaciones']; 
                $asignado = $_POST['observaciones'];   

                try{
                    $customers = new Application_Model_DbTable_Vehicle();
                   $customers->updateVehicle($placa,$anno,$marca,$modelo,$motor,$observaciones,$asignado);
                    
                    $this->_helper->redirector('index');
                }
                catch(Exception $e){
                    throw $e;
                }
            }
       
        }

    }

    public function deleteAction()
    {
        // action body
         $placa = $this->getRequest()->getPost('placa');
        try{
            $customers = new Application_Model_DbTable_Vehicle();
            $customers->deleteVehicle($placa);
            $this->_helper->redirector('index');
        }
        catch(Exception $e){  
        echo $e;        
            throw $e;
        }
    }

    public function addbrandvehicleAction()
    {
        // action body
        
    }

    public function findbrandvehicleAction()
    {

    }
 public function pruebaunoAction()
    {

    }
    public function deletebrandvehicleAction()
    {
        // action body
    }

    public function editbrandvehicleAction()
    {
        // action body
    }


}